package org.prj;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaFileObject;

import com.innowhere.relproxy.RelProxyOnReloadListener;
import com.innowhere.relproxy.jproxy.JProxy;
import com.innowhere.relproxy.jproxy.JProxyConfig;
import com.innowhere.relproxy.jproxy.JProxyDiagnosticsListener;

public class InitServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 716351356115352591L;
	//ServiceManager serviceManager;
	
	@Override
	public void init(ServletConfig config) throws ServletException {		
		super.init(config);
		
		ServletContext context = config.getServletContext();
	    String inputPath = context.getRealPath("/") + "WEB-INF/code/";
	    String classFolder = context.getRealPath("/") + "WEB-INF/classes";
	    Iterable<String> compilationOptions = Arrays.asList(new String[]{"-source","1.6","-target","1.6"});
	    long scanPeriod = -1;

	    RelProxyOnReloadListener proxyListener = new RelProxyOnReloadListener() {
			
			@Override
	        public void onReload(Object objOld, Object objNew, Object proxy, Method method, Object[] args) {
	            System.out.println("Reloaded " + objNew + " Calling method: " + method);
	        }
	    };

	    JProxyDiagnosticsListener diagnosticsListener = new JProxyDiagnosticsListener()
	    {
	    	
	        public void onDiagnostics(DiagnosticCollector<JavaFileObject> diagnostics)
	        {
	            List<Diagnostic<? extends JavaFileObject>> diagList = diagnostics.getDiagnostics();
	            int i = 1;
	            for (Diagnostic diagnostic : diagList)
	            {
	               System.out.println("Diagnostic " + i);
	               System.out.println("  code: " + diagnostic.getCode());
	               System.out.println("  kind: " + diagnostic.getKind());
	               System.out.println("  line number: " + diagnostic.getLineNumber());
	               System.out.println("  column number: " + diagnostic.getColumnNumber());
	               System.out.println("  start position: " + diagnostic.getStartPosition());
	               System.err.println("  position: " + diagnostic.getPosition());
	               System.err.println("  end position: " + diagnostic.getEndPosition());
	               System.err.println("  source: " + diagnostic.getSource());
	               System.err.println("  message: " + diagnostic.getMessage(null));
	               i++;
	            }
	        }
	    };

	    JProxyConfig jpConfig = JProxy.createJProxyConfig();
	    jpConfig.setEnabled(true)
	            .setRelProxyOnReloadListener(proxyListener)
	            .setInputPath(inputPath)
	            .setScanPeriod(scanPeriod)
	            .setClassFolder(classFolder)
	            .setCompilationOptions(compilationOptions)
	            .setJProxyDiagnosticsListener(diagnosticsListener);

	    JProxy.init(jpConfig);
	    
	    JProxy.start();


	    //String pathPrefix = context.getRealPath("/") + "/WEB-INF/javaex/pages/";

	   

	    
	    
	    //serviceManager.init();
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		//getServiceManager().init();
		
		resp.getWriter().write("ok");
		resp.getWriter().flush();
		resp.getWriter().close();
	}

//	public ServiceManager getServiceManager() {
//		if(serviceManager==null)
//			serviceManager = JProxy.create(new ServiceManagerImpl(), ServiceManager.class);
//		return serviceManager;
//	}
//
//	public void setServiceManager(ServiceManager serviceManager) {
//		this.serviceManager = serviceManager;
//	}
	
	
	
}
